library(foreign)
library(ggplot2)
library(ggridges)
library(RColorBrewer)

cols<-c("#009E73", "#E69F00")
cols4<-c("grey0", "grey17", "grey34", "grey51")





#### 
# Figure 1
# Satisfaction by winner and year
####
dev.new(width=3,height=3)
dev.size()
satis<-read.dta("*/Replication files/Predicted values/satis by year.dta")
satis$Winner<-as.factor(satis$winner)

ggplot(satis, aes(x=year, y= satis)) + 
	geom_linerange(mapping=aes(x=year,ymin=lower,ymax=upper,color=winner),size=.33) +
	geom_point(aes(y=satis,group=winner,shape=winner,color=winner,fill=winner),size=1.667) + 
 	scale_shape_manual(values = c(21, 21)) +
 	scale_fill_manual(values = c("white", "orange")) + 
 	scale_color_manual(values = c("#009E73", "orange")) + 
 	theme_linedraw()  + 
	theme(panel.grid.minor = element_blank()) +
	theme(panel.grid.major = element_blank()) +
	#theme(panel.grid.major = element_line(colour = "grey",size=0.5, linetype="solid")) + 
	theme(axis.text.y = element_text(angle = 0 , color="black" , hjust=)) +
	theme(axis.text.x = element_text(color="black")) +
	ylab("Satisfaction with Democracy") +
	xlab(" ") +
	scale_x_continuous(expand = c(0.05, 0.05), breaks=c(2004,2008,2012,2016)) +
	theme(legend.position = "none") +
	theme(aspect.ratio=1) + 
    theme(panel.border = element_rect(fill=NA, colour = "black", size=.667)) +
    theme(strip.background = element_rect(color="white",fill="white",size=1)) + 
  	theme(strip.text = element_text(color = 'black')) 


#### 
# Figure 2
# Racial resentment by winner
####
rr<-read.dta("*/Replication files/Predicted values/rr by winner.dta")
rr$winner<-as.factor(rr$winner)
 
dev.new(width=6,height=3)
dev.size()
coul = brewer.pal(2, "Blues")
coul = colorRampPalette(coul)(2)   


ggplot(rr, aes(x = racial_resentment, y = as.numeric(winner), fill = winner)) +
	geom_density_ridges(alpha = 1/2, show.legend = FALSE , size=.25) +	
	facet_grid(~year) + 
	scale_x_continuous(expand = c(0.025, .025), breaks=c(1,5),
		labels=c("Low", "High")) +	
	scale_y_continuous(expand = c(0.025, .025), breaks=c(1,2),
		labels=c("Losers", "Winners")) +
	theme(legend.position = "none") +
	theme_linedraw() +
	scale_fill_manual(values = cols, guide = "none") +
#	scale_fill_brewer(palette = "Blues")+
	theme(panel.grid.minor = element_blank()) +
	theme(panel.grid.major = element_line(colour = "grey",size=0.5, linetype="solid")) + 
	theme(axis.text.y = element_text(angle = 0 , color="black" , hjust=)) +
	theme(axis.text.x = element_text(color="black")) +
	xlab("Racial resentment") +
	ylab("Density") +
	theme(aspect.ratio=1) + 
	theme(panel.grid.major.x = element_blank()) +
    theme(panel.border = element_rect(fill=NA, colour = "black", size=.667)) +
    theme(strip.background = element_rect(color="white",fill="white",size=1)) + 
  	theme(strip.text = element_text(color = 'black',vjust=-.5))


#### 
# Figure 3
# Predicted value of satisfaction by rr, conditioned on winner and year
# OLS
####
dev.new(width=6,height=3)
dev.size()

rrp<-read.csv("*/Replication files/Predicted values/predicted values.csv")
rrp$winner<-as.factor(rrp$winner)

p <- ggplot(rrp, aes(x=rr,group=winner)) +
 	geom_line(aes(y=y, linetype= winner, color= winner)) + 	geom_ribbon(aes(x=rr,ymin= rrp$lower, ymax= rrp$upper,fill= winner),alpha=.2)
 p + facet_grid(~ year) +
  	xlab("Racial Resentment") +
	ylab("Satisfaction") +
	theme_linedraw() +
#	theme(legend.position="bottom", legend.box = "horizontal") +
	theme(legend.title = element_blank())+
	scale_fill_manual(values = cols) +
	scale_color_manual(values = cols) +
  	theme(aspect.ratio=.85) +
  	theme(panel.grid.minor = element_blank()) + 
	theme(panel.grid.major = element_blank()) +
	theme(panel.grid.major.y = element_line(colour = "grey90",size=0.5, linetype="solid")) + 
	theme(strip.background = element_rect(color="white",fill="white",size=1)) +   
   	theme(strip.text = element_text(color = 'black')) +	
   	scale_x_continuous(expand = c(0.0025, 0.0025), breaks=c(2,16),		labels=c("Low", "High")) +    	
   	theme(panel.spacing = unit(.667, "lines"))


####
# Figure 4
# 2016 validated voters (OLS)
####
dev.new(width=3,height=3)
dev.size()
rrp.v<-read.csv("*/Replication files/Predicted values/2016 validated pr.csv")
rrp.v$winner<-as.factor(rrp.v$winner)


p <- ggplot(rrp.v, aes(x=rr,group=winner)) +
 	geom_line(aes(y=y, linetype= winner, color= winner)) + 	geom_ribbon(aes(x=rr,ymin= lower, ymax= upper,fill= winner),alpha=.2)
p +
  	xlab("Racial Resentment") +
	ylab("Satisfaction") +
	theme_linedraw() +
	theme(legend.position = "none") +
	scale_fill_manual(values = cols) +
	scale_color_manual(values = cols) +
  	theme(aspect.ratio=.85) +
  	theme(panel.grid.minor = element_blank()) + 
	theme(panel.grid.major = element_blank()) +
	theme(strip.background = element_rect(color="white",fill="white",size=1)) +   
   	theme(strip.text = element_text(color = 'black')) +	
   	scale_x_continuous(expand = c(0.0025, 0.0025), breaks=c(2,16),		labels=c("Low", "High")) +    	
   	theme(panel.spacing = unit(.667, "lines"))

####
# Figure 5
# Panel data (OLS)
####
dev.new(width=3,height=3)
dev.size()
rrp.p<-read.csv("*/Replication files/Predicted values/panel results.csv")
rrp.p$winner<-as.factor(rrp.p$winner)
p <- ggplot(rrp.p, aes(x=rr,group=winner)) +
 	geom_line(aes(y=y, linetype= winner, color= winner)) + 	geom_ribbon(aes(x=rr,ymin= lower, ymax= upper,fill= winner),alpha=.2)
p +
  	xlab("Racial Resentment") +
	ylab("Satisfaction") +
	theme_linedraw() +
	theme(legend.position = "none") +
	scale_fill_manual(values = cols) +
	scale_color_manual(values = cols) +
  	theme(aspect.ratio=.85) +
  	theme(panel.grid.minor = element_blank()) + 
	theme(panel.grid.major = element_blank()) +
	theme(strip.background = element_rect(color="white",fill="white",size=1)) +   
   	theme(strip.text = element_text(color = 'black')) +	
   	scale_x_continuous(expand = c(0.0025, 0.0025), breaks=c(2,16),		labels=c("Low", "High")) +    	
   	theme(panel.spacing = unit(.667, "lines"))   	


########################
# Appendix figures OLS #
########################

####
# Compare predicted values cross-sectional models
####

compare<-read.csv("*/Replication files/Predicted values/compare_pred_values_ols.csv")

compare$winner<-as.factor(compare$winner)


p <- ggplot(compare, aes(x=rr, y=y, colour=interaction(winner, model), linetype = interaction(winner, model),
  group=interaction(winner, model))) + geom_line()

 p + facet_grid(~ year ) +
scale_colour_manual(name = "Winner status & Model", labels = c("Electoral losers full sample, adjusts for consistency","Electoral winners full sample, adjusts for consistency","Electoral losers, main model","Electoral losers, main model", "Electoral losers, post-election" , "Electoral winners, post election"),values=c("red", "blue", "red", "blue", "red", "blue"))  + 

scale_linetype_manual(name = "Winner status & Model", labels = c("Electoral losers full sample, adjusts for consistency","Electoral winners full sample, adjusts for consistency","Electoral losers, main model","Electoral losers, main model", "Electoral losers, post-election" , "Electoral winners, post election"),values=c(1 , 1, 2, 2, 3, 3)) + 
	ylab("Satisfaction") +
  	xlab("Racial Resentment") +
	theme_linedraw() +
	#theme(legend.position="bottom", legend.box = "vertical") + guides(col = guide_legend(ncol = 1)) +  
  	theme(aspect.ratio=.85) +
  	theme(panel.grid.minor = element_blank()) + 
	theme(panel.grid.major = element_blank()) +
	theme(panel.grid.major.y = element_line(colour = "grey90",size=0.5, linetype="solid")) + 
	theme(strip.background = element_rect(color="white",fill="white",size=1)) +   
   	theme(strip.text = element_text(color = 'black')) +	
   	scale_x_continuous(expand = c(0.0025, 0.0025), breaks=c(2,16),		labels=c("Low", "High")) +    	
   	theme(panel.spacing = unit(.667, "lines"))

####
# Compare predicted values cross-sectional 2016 — compares full data with validated sample
# OLS
####

gg_color_hue <- function(n) {
  hues = seq(15, 375, length = n + 1)
  hcl(h = hues, l = 65, c = 100)[1:n]
}
n = 4
cols = gg_color_hue(n)

compare.s<-read.csv("*/Replication files/Predicted values/compare 2016 ols.csv")
compare.s$winner<-as.factor(compare.s$winner)


p <- ggplot(compare.s, aes(x=rr, y=y, colour=interaction(winner,Sample), linetype = interaction(winner,Sample),   group=interaction(winner, Sample))) + geom_line()

p + 
scale_colour_manual(name = "Winner status & Model", labels = c("Electoral losers full sample","Electoral winners full sample","Electoral losers validated sample","Electoral winners validated sample"),values=cols)  + 

scale_linetype_manual(name = "Winner status & Model", labels = c("Electoral losers full sample","Electoral winners full sample","Electoral losers validated sample","Electoral winners validated sample"),values=c(1, 2, 3, 4) )+ 
	ylab("Probability") +
  	xlab("Racial Resentment") +
	theme_linedraw() +
	theme(legend.position="bottom", legend.box = "vertical") + guides(col = guide_legend(ncol = 1)) +  
  	theme(aspect.ratio=.85) +
  	theme(panel.grid.minor = element_blank()) + 
	theme(panel.grid.major = element_blank()) +
	theme(strip.background = element_rect(color="white",fill="white",size=1)) +   
   	theme(strip.text = element_text(color = 'black')) +	
   	scale_x_continuous(expand = c(0.0025, 0.0025), breaks=c(2,16),		labels=c("Low", "High")) +    	
   	theme(panel.spacing = unit(.667, "lines"))

####
# Compare predicted values panel data
# OLS
####

gg_color_hue <- function(n) {
  hues = seq(15, 375, length = n + 1)
  hcl(h = hues, l = 65, c = 100)[1:n]
}
n = 4
cols = gg_color_hue(n)

compare.p<-read.csv("*/Replication files/Predicted values/compare 2004 ols.csv")
compare.p$winner<-as.factor(compare.p $winner)
compare.p$sample<-as.factor(compare.p $sample)

p + 
scale_colour_manual(name = "Winner-loser status & Sample", labels = c("Electoral losers 2000 to 2004 Merged File","Electoral winners 2000 to 2004 Merged File","Electoral losers 2004 Time Series","Electoral winners 2004 Time Series"),values=cols)  + 
 scale_linetype_manual(name = "Winner-loser status & Sample", labels = c("Electoral losers 2000 to 2004 Merged File","Electoral winners 2000 to 2004 Merged File","Electoral losers 2004 Time Series","Electoral winners 2004 Time Series") , values=c(1,2,3,4))  +
  	xlab("Racial Resentment") +
	ylab("Satisfaction") +
	theme_linedraw() +
  	theme(aspect.ratio=.85) +
  	theme(panel.grid.minor = element_blank()) + 
	theme(panel.grid.major = element_blank()) +
	theme(strip.background = element_rect(color="white",fill="white",size=1)) +   
   	theme(strip.text = element_text(color = 'black')) +	
   	scale_x_continuous(expand = c(0.0025, 0.0025), breaks=c(2,16),		labels=c("Low", "High")) +    	
   	theme(panel.spacing = unit(.667, "lines"))

####
# 2008 without interet
####
no.interest<-read.csv("*/Replication files/Predicted values/2008withoutinterestMFX.csv")
no.interest$winner<-as.factor(no.interest$winner)
p <- ggplot(no.interest, aes(x=rr,group=winner)) +
 	geom_line(aes(y=y, linetype= winner, color= winner)) + 	geom_ribbon(aes(x=rr,ymin= lower, ymax= upper,fill= winner),alpha=.2)
p + facet_grid(~model) +
  	xlab("Racial Resentment") +
	ylab("Satisfaction") +
	theme_linedraw() +
	theme(legend.position="bottom", legend.box = "horizontal") +
	scale_fill_manual(values = cols) +
	scale_color_manual(values = cols) +
  	theme(aspect.ratio=.85) +
  	theme(panel.grid.minor = element_blank()) + 
	theme(panel.grid.major = element_blank()) +
	theme(strip.background = element_rect(color="white",fill="white",size=1)) +   
   	theme(strip.text = element_text(color = 'black')) +	
   	scale_x_continuous(expand = c(0.0025, 0.0025), breaks=c(2,16),		labels=c("Low", "High")) +    	
   	theme(panel.spacing = unit(.667, "lines")) 



#############################
# Old ordered logit figures #
#############################

#### 
# Predicted value of satisfaction by rr, conditioned on winner and year
# (ologit)
####

rrp<-read.csv("*/Replication files/Predicted values/predicted values ologit.csv")
rrp$winner<-as.factor(rrp$winner)
rrp$Satisfaction<-reorder(rrp$Satisfaction, rrp$satis)
rrp$satis<-as.factor(rrp$satis)

p <- ggplot(rrp, aes(x=rr,group=Satisfaction)) +
 	geom_line(aes(y=y, linetype= Satisfaction, color= Satisfaction)) + 	geom_ribbon(aes(x=rr,ymin= rrp$lower, ymax= rrp$upper,fill= Satisfaction),alpha=.2)
 p + facet_grid(winner ~ year) +
  	xlab("Racial Resentment") +
	ylab("Probability") +
	theme_linedraw() +
	theme(legend.position="bottom", legend.box = "horizontal") +
	scale_fill_manual(values = cols) +
	scale_color_manual(values = cols) +
  	theme(aspect.ratio=.85) +
  	theme(panel.grid.minor = element_blank()) + 
	theme(panel.grid.major = element_blank()) +
	theme(panel.grid.major.y = element_line(colour = "grey90",size=0.5, linetype="solid")) + 
	theme(strip.background = element_rect(color="white",fill="white",size=1)) +   
   	theme(strip.text = element_text(color = 'black')) +	
   	scale_x_continuous(expand = c(0.0025, 0.0025), breaks=c(2,16),		labels=c("Low", "High")) +    	
   	theme(panel.spacing = unit(.667, "lines"))
   	
####
# 2016 validated voters (ologit)
####
rrp.v<-read.csv("*/Replication files/Predicted values/2016 validated pr ologit.csv")
rrp.v$winner<-as.factor(rrp.v$winner)
rrp.v$Satisfaction<-reorder(rrp.v$Satisfaction, rrp.v$satis)
rrp.v$satis<-as.factor(rrp.v$satis)


p <- ggplot(rrp.v, aes(x=rr,group=Satisfaction)) +
 	geom_line(aes(y=pr, linetype= Satisfaction, color= Satisfaction)) + 	geom_ribbon(aes(x=rr,ymin= lower, ymax= upper,fill= Satisfaction),alpha=.2)
p + facet_grid(~winner) +
  	xlab("Racial Resentment") +
	ylab("Probability") +
	theme_linedraw() +
	theme(legend.position="bottom", legend.box = "horizontal") +
	scale_fill_manual(values = cols) +
	scale_color_manual(values = cols) +
  	theme(aspect.ratio=.85) +
  	theme(panel.grid.minor = element_blank()) + 
	theme(panel.grid.major = element_blank()) +
	theme(panel.grid.major.y = element_line(colour = "grey90",size=0.5, linetype="solid")) + 
	theme(strip.background = element_rect(color="white",fill="white",size=1)) +   
   	theme(strip.text = element_text(color = 'black')) +	
   	scale_x_continuous(expand = c(0.0025, 0.0025), breaks=c(2,16),		labels=c("Low", "High")) +    	
   	theme(panel.spacing = unit(.667, "lines"))
   	
###########################
# Appendix figures ologit #
###########################

####
# Compare predicted values cross-sectional models
####

compare<-read.csv("*/Replication files/Predicted values/compare_pred_values.csv")

compare$winner<-as.factor(compare$winner)
compare$Satisfaction<-reorder(compare$Satisfaction, compare$satis)
compare$satis<-as.factor(compare$satis)


p <- ggplot(compare, aes(x=rr, y=y, color=interaction(winner, model), linetype = interaction(winner, model),
  group=interaction(winner, model))) + geom_line()

 p + facet_grid(Satisfaction ~ year ) +
scale_colour_manual(name = "Winner status & Model", labels = c("Electoral losers full sample, adjusts for consistency","Electoral winners full sample, adjusts for consistency","Electoral losers, main model","Electoral losers, main model", "Electoral losers, post-election" , "Electoral winners, post election"),values=c("red", "blue", "red", "blue", "red", "blue"))  + 

scale_linetype_manual(name = "Winner status & Model", labels = c("Electoral losers full sample, adjusts for consistency","Electoral winners full sample, adjusts for consistency","Electoral losers, main model","Electoral losers, main model", "Electoral losers, post-election" , "Electoral winners, post election"),values=c(1 , 1, 2, 2, 3, 3)) + 
	ylab("Satisfaction") +
  	xlab("Racial Resentment") +
	theme_linedraw() +
	theme(legend.position="bottom", legend.box = "vertical") + #guides(col = guide_legend(ncol = 1)) +  
  	theme(aspect.ratio=.85) +
  	theme(panel.grid.minor = element_blank()) + 
	theme(panel.grid.major = element_blank()) +
	theme(panel.grid.major.y = element_line(colour = "grey90",size=0.5, linetype="solid")) + 
	theme(strip.background = element_rect(color="white",fill="white",size=1)) +   
   	theme(strip.text = element_text(color = 'black')) +	
   	scale_x_continuous(expand = c(0.0025, 0.0025), breaks=c(2,16),		labels=c("Low", "High")) +    	
   	theme(panel.spacing = unit(.667, "lines"))

####
# Compare predicted values cross-sectional 2016 — compares full data with validated sample
####

gg_color_hue <- function(n) {
  hues = seq(15, 375, length = n + 1)
  hcl(h = hues, l = 65, c = 100)[1:n]
}
n = 4
cols = gg_color_hue(n)

compare.s<-read.csv("*/Replication files/Predicted values/compare 2016.csv")
compare.s$winner<-as.factor(compare.s$winner)
compare.s$Satisfaction<-reorder(compare.s$Satisfaction, compare.s$satis)
compare.s$satis<-as.factor(compare.s$satis)


p <- ggplot(compare.s, aes(x=rr, y=pr, colour=interaction(winner,Sample), linetype = interaction(winner,Sample),   group=interaction(winner, Sample))) + geom_line()

p + facet_grid(~ Satisfaction) +
scale_colour_manual(name = "Winner status & Model", labels = c("Electoral losers full sample","Electoral winners full sample","Electoral losers validated sample","Electoral winners validated sample"),values=cols)  + 

scale_linetype_manual(name = "Winner status & Model", labels = c("Electoral losers full sample","Electoral winners full sample","Electoral losers validated sample","Electoral winners validated sample"),values=c(1, 2, 3, 4) )+ 
	ylab("Probability") +
  	xlab("Racial Resentment") +
	theme_linedraw() +
	theme(legend.position="bottom", legend.box = "vertical") + guides(col = guide_legend(ncol = 1)) +  
  	theme(aspect.ratio=.85) +
  	theme(panel.grid.minor = element_blank()) + 
	theme(panel.grid.major = element_blank()) +
	theme(panel.grid.major.y = element_line(colour = "grey90",size=0.5, linetype="solid")) + 
	theme(strip.background = element_rect(color="white",fill="white",size=1)) +   
   	theme(strip.text = element_text(color = 'black')) +	
   	scale_x_continuous(expand = c(0.0025, 0.0025), breaks=c(2,16),		labels=c("Low", "High")) +    	
   	theme(panel.spacing = unit(.667, "lines"))
   	
####
# Compare predicted values panel data
####

gg_color_hue <- function(n) {
  hues = seq(15, 375, length = n + 1)
  hcl(h = hues, l = 65, c = 100)[1:n]
}
n = 4
cols = gg_color_hue(n)

compare.p<-read.csv("*/Replication files/Predicted values/compare 2004.csv")
compare.p$winner<-as.factor(compare.p $winner)
compare.p$Satisfaction<-reorder(compare.p $Satisfaction, compare.p $satis)
compare.p$satis<-as.factor(compare.p $satis)

p <- ggplot(compare.p, aes(x=rr, y=y, colour=interaction(winner,sample), linetype = interaction(winner,sample),   group=interaction(winner, sample))) + geom_line()

p + facet_grid(~Satisfaction) +
scale_colour_manual(name = "Winner-loser status & Sample", labels = c("Electoral losers 2000 to 2004 Merged File","Electoral winners 2000 to 2004 Merged File","Electoral losers 2004 Time Series","Electoral winners 2004 Time Series"),values=cols)  + 
 scale_linetype_manual(name = "Winner-loser status & Sample", labels = c("Electoral losers 2000 to 2004 Merged File","Electoral winners 2000 to 2004 Merged File","Electoral losers 2004 Time Series","Electoral winners 2004 Time Series") , values=c(1,2,3,4))  +
  	xlab("Racial Resentment") +
	ylab("Probability") +
	theme_linedraw() +
	theme(legend.position="bottom", legend.box = "vertical") + guides(col = guide_legend(ncol = 1)) +  
  	theme(aspect.ratio=.85) +
  	theme(panel.grid.minor = element_blank()) + 
	theme(panel.grid.major = element_blank()) +
	theme(panel.grid.major.y = element_line(colour = "grey90",size=0.5, linetype="solid")) + 
	theme(strip.background = element_rect(color="white",fill="white",size=1)) +   
   	theme(strip.text = element_text(color = 'black')) +	
   	scale_x_continuous(expand = c(0.0025, 0.0025), breaks=c(2,16),		labels=c("Low", "High")) +    	
   	theme(panel.spacing = unit(.667, "lines"))
